package hhpoll.validation;

import hhpoll.domain.User;

import org.apache.commons.validator.EmailValidator;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class UserValidator implements Validator {
	
	private PasswordValidator passwordVal;
	
	public UserValidator() {
		passwordVal = new PasswordValidator();
	}
	

	@Override
	public boolean supports(Class clazz) {
		return User.class.equals(clazz);
	}

	@Override
	public void validate(Object obj, Errors errors) {
		User user = (User)obj;
        ValidationUtils.rejectIfEmpty(errors, "email", "email.empty");
        ValidationUtils.rejectIfEmpty(errors, "password", "password.empty");
        ValidationUtils.rejectIfEmpty(errors, "firstName", "firstName.empty");
        ValidationUtils.rejectIfEmpty(errors, "lastName", "lastName.empty");
        //EmailValidator emailValidator = EmailValidator.getInstance();
        //if (!emailValidator.isValid(user.getEmail()))
        //	errors.rejectValue("email", "email.invalid");
        ValidationUtils.invokeValidator(passwordVal, user.getPassword(), errors);
	}

}
